﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Pex.Framework;


namespace Benchmarks.McMinnThesisBen
{
    [TestClass]
    public class SmokeDetector
    {
        static int LEVEL = 300;
        static int DANGER = 3;
        static int WAIT_TIME = 3;
        
        int time;
        int off_time;
        int detected;
        bool alarm_on;
        bool waiting;

        bool signal_on;
        bool signal_off;

        public SmokeDetector()
        {
            time = 0;
            off_time = 0;
            detected = 0;
            alarm_on = false;
            waiting = false;

            signal_on = false;
            signal_off = false;
        }

        public void smoke_detector(int level)
        {
            time++;

            if (level > LEVEL && detected < DANGER)
            {
                detected++;
            }
            else if (level <= LEVEL && detected > 0)
            {
                detected--;
            }
            if (!alarm_on && detected == DANGER)
            {
                alarm_on = true;
                signal_on = true;
            }
            if (alarm_on)
            {
                if (!waiting && detected == 0)
                {
                    waiting = true;
                    off_time = time + WAIT_TIME;
                }
                if (waiting && detected == DANGER)
                {
                    waiting = false;
                }
                if (waiting && time >= off_time)
                {
                    waiting = false;
                    alarm_on = false;
                    signal_off = true;
                }
            }
        }

        //run with C:\pex\bin\Debug>Pex Benchmarks.dll /mf:TestSmokeDetectorFitness
        // adding /fte outputs all generated tests
        //[PexMethod(MaxRuns = 100, MaxConditions = int.MaxValue, MaxRunsWithoutNewTests = int.MaxValue)]        
        //public void TestSmokeDetectorFitness(int[] levels)
        //{
        //    TestSmokeDetector(levels);
        //}

        ////run with C:\pex\bin\Debug>Pex Benchmarks.dll /mf:TestArrayDiffDefault
        //// adding /fte outputs all generated tests
        //[PexMethod(MaxRuns = 100, MaxConditions = int.MaxValue, MaxRunsWithoutNewTests = int.MaxValue)]
        //public void TestSmokeDetectorDefault(int[] levels)
        //{
        //    TestSmokeDetector(levels);
        //}

        //private void TestSmokeDetector(int[] levels)
        //{
        //    PexAssume.IsTrue(levels != null);

        //    SmokeDetector c = new SmokeDetector();
        //    for (int i = 0; i <= (levels.Length - 1); i++)
        //        c.smoke_detector(levels[i]);
        //}
    }
}
